package com.hex.liyue.storemodule.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hex.liyue.R;
import com.hex.liyue.commonmodule.activity.BaseActivity;
import com.hex.liyue.commonmodule.utils.StatusBarCompatUtils;
import com.hex.liyue.commonmodule.view.scrollbanner.BannerViewPager;
import com.hex.liyue.storemodule.bean.FragmentStoreBean;

import java.io.Serializable;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by WJ on 2016/6/15.
 * 这是一个商品详情的界面
 */
public class StoreDetailsActivity extends BaseActivity implements Serializable {
    @Bind(R.id.iv_toobar_back)
    ImageView ivToobarBack;
    @Bind(R.id.tv_toobar_title)
    TextView tvToobarTitle;

    @Bind(R.id.bn_store_details_banner)
    BannerViewPager bnStoreDetailsBanner;//轮播图
    @Bind(R.id.iv_store_detils)
    ImageView mIvStoreDetils;//单个图
    @Bind(R.id.tv_store_brand)
    TextView tvStoreBrand;//品牌
    @Bind(R.id.tv_store_name)
    TextView tvStoreName;//名字
    @Bind(R.id.tv_store_prices)
    TextView tvStorePrice;//单价
    @Bind(R.id.tv_store_num)
    TextView tvStroeNum;//购买数量
    @Bind(R.id.bt_store_details_pay)
    Button mBtStorePay;//支付
    @Bind(R.id.ll_store_detalis)
    LinearLayout mLlStoreDetails;// + - 数量盒子


    private List<ImageView> views;//图片组件
    private List<String> infos;//图片URL地址
    private int num = 1;//操作数量
    private FragmentStoreBean.DataBean dataBean;//商品详情
    private int position;


    @Override
    protected void bindPresenter() {

    }

    @Override
    protected void initData(Bundle extras) {
        bnStoreDetailsBanner.setVisibility(View.GONE);
        boolean isOtherRun = extras.getBoolean("isOtherRun", false);
        if (isOtherRun) {
            //其他地方进入的商品详情
            mLlStoreDetails.setVisibility(View.GONE);
            mBtStorePay.setVisibility(View.GONE);
        } else {
            //商品进入的
            mLlStoreDetails.setVisibility(View.VISIBLE);
            mBtStorePay.setVisibility(View.VISIBLE);
        }
        dataBean = (FragmentStoreBean.DataBean) getIntent().getSerializableExtra("dataBean");
        Glide.with(this)
                .load(dataBean.gift_url)
                .error(R.drawable.default_error_image)
                .into(mIvStoreDetils);
        tvStorePrice.setText("￥" + dataBean.price);
        tvStoreBrand.setText(dataBean.gift_brand);
        tvStoreName.setText(dataBean.gift_name);
        position = getIntent().getIntExtra("position", 0);
        //显示单图
        mIvStoreDetils.setVisibility(View.VISIBLE);
        Glide.with(this).load(dataBean.gift_url).centerCrop().error(R.drawable.default_error).into(mIvStoreDetils);
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_store_details;
    }

    @Override
    protected void initToolbar() {
        StatusBarCompatUtils.compat(this);
        ivToobarBack.setVisibility(View.VISIBLE);
        tvToobarTitle.setText("商品详情");
    }


    @OnClick({R.id.iv_toobar_back, R.id.bt_store_details_pay, R.id.bt_store_min, R.id.bt_store_max})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_toobar_back:
                onBackPressed();
                break;
            case R.id.bt_store_details_pay://跳转到订单确认界面
                if (dataBean != null) {
                    Intent intent = new Intent(StoreDetailsActivity.this, StoreSureOrderActivity.class);
                    intent.putExtra("dataBean", dataBean);
                    intent.putExtra("num", num);
                    startActivityForResult(intent, 1);
                }

                break;
            case R.id.bt_store_max://添加数量的按钮
                addNum();
                break;
            case R.id.bt_store_min://减少数量的按钮
                deleteNum();
                break;
        }
    }

    private void addNum() {
        num++;
        if (num == 100) {
            num--;
        }
        tvStroeNum.setText("" + num);
    }

    private void deleteNum() {
        num--;
        if (num == 0) {
            num++;
        }
        tvStroeNum.setText("" + num);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 1) {
                setResult(RESULT_OK);
                finish();
            }
        }
    }
}
